# -*- coding: utf-8 -*-
# @Time : 2023/11/29 13:47:03
# @Author : sacon
# @File : phone.py

import random
import requests
import time
from loguru import logger

from Api.BookAccountNumberManage.retry_model import MyRetry


class Phone():
    def __init__(self):
        self.Api_domain_list = ['http://api.yjyfdc.com','http://api.sdkjfz.com','http://api.cndqlm.com']

        self.username = 'xuxingxing'
        self.password = 'xxx123456'

        self.authtoken = self.login()
        if not self.authtoken:
            raise ValueError('登录失败')

    @MyRetry(times=5, return_obj=False)
    def login(self):
        url = f'{random.choice(self.Api_domain_list)}/api/login?account={self.username}&password={self.password}&loginsource=API'

        res = requests.get(url,timeout=20)
        if res.status_code == 200:
            return res.json()['data']['authtoken']
        else:
            logger.error(f'登录失败:{res.text}')
            raise ValueError('登录失败')

    @MyRetry(times=5, return_obj=(False,False))
    def get_phone(self,item_id='7256'):
        url = f'{random.choice(self.Api_domain_list)}/api/phone/loadReceivePhone?Authorization={self.authtoken}&itemid={item_id}&itemtype=0&phonetype=1'
        res = requests.get(url,timeout=20)
        if res.status_code == 200:
            comid = res.json()['data']['comid']
            phone = res.json()['data']['itemlist']['phone']
            return comid,phone
        else:
            logger.error(f'获取手机号失败:{res.text}')
            raise ValueError('获取手机号失败')

    @MyRetry(times=5, return_obj=False)
    def get_phone_code(self,comid):
        url = f'{random.choice(self.Api_domain_list)}/api/phone/getPutSmsResult?Authorization={self.authtoken}&comid={comid}'
        for i in range(30):
            res = requests.get(url,timeout=20)
            if res.status_code == 200:
                if res.json()['code'] == 1:
                    code = res.json()['data']['comdata']['com_code']
                    if not code:
                        time.sleep(5)
                        continue
                    return code
                else:
                    logger.debug(f'暂未获取到验证码，等待5秒后继续获取 :{res.text} ')
                    time.sleep(5)

            else:
                logger.error(f'获取验证码失败:{res.text}')
                raise ValueError('获取验证码失败')
        return False

    @MyRetry(times=5, return_obj=False)
    def release_phone(self,phone,item_id='7256'):
        url = f'{random.choice(self.Api_domain_list)}/api/phone/submitRelasePhone?Authorization={self.authtoken}&itemid={item_id}&phone={phone}'
        res = requests.get(url,timeout=20)
        if res.status_code == 200:
            # comid = res.json()['data']['comid']
            # phone = res.json()['data']['phone']
            # return comid,phone
            logger.debug(f'释放手机号成功:{res.text}')
            return True
        else:
            logger.error(f'获取手机号失败:{res.text}')
            raise ValueError('获取手机号失败')


phone_obj = Phone()



















